package com.example.a24porint;

import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class lookrankwindow extends AppCompatActivity {
    public Socket socket=null;
    private OutputStream outputStream;
    private InputStream inputStream;
    private ListView ol;
    private BufferedReader in;
    private class rankcontent
    {
        String name="";
        int win=0;
        int lose=0;
    }
    class mycompare implements Comparator<rankcontent> {
        @Override
        public int compare(rankcontent o1, rankcontent o2) {
            if(o1.win == o2.win){
                return o1.lose < o2.lose?-1:1;	//返回lose较大的
            }
            return o1.win > o2.win?-1:1;	//返回win较大的
        }
    }
    private String[] getonlineuser(String str)//"online:askojdfoa,asdfijoasd,asdjfo,voasjdfoi,erwafopisdj;\r";
    {
        String[] arr;
        int p=0;
        List list = new ArrayList();
        if(str==null||str.length()==0)return null;
        String name="";

        for(int i=0;i<str.length();i++)
        {
            if(str.charAt(i)==';')
            {
                list.add(name);
                name="";
            }
            else
            {
                name+=str.charAt(i);
            }
        }
        rankcontent[] rc=new rankcontent[list.size()];
        for(int i=0;i<list.size();i++)
        {
            String s= (String) list.get(i);
            int douhao=0;
            for(p=0;p<s.length();p++)
            {
                if(s.charAt(p)!=',') {
                    if (douhao == 0) {
                        rc[i].name += s.charAt(i);
                    }
                    else if(douhao==1){
                        rc[i].win*=10;
                        rc[i].win+=s.charAt(i)-'0';
                    }
                    else if(douhao==2){
                        rc[i].lose*=10;
                        rc[i].lose+=s.charAt(i)-'0';
                    }
                }
                else{
                    douhao++;
                }
            }
        }
        Arrays.sort(rc, 1, list.size(), new mycompare());
        arr=(String[])list.toArray(new String[list.size()]);
        return arr;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lookrankwindow);
        ol=findViewById(R.id.ranklist);
        ApplicationUtil appUtil = (ApplicationUtil) lookrankwindow.this.getApplication();
        try {
            appUtil.init();
            socket = appUtil.getSocket();
            outputStream = appUtil.getOutputStream();
            inputStream = appUtil.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("initerrortag", "wjoafjaoisdjfoiasdjf--------------------");
        }
        final String[] receiveMsg = new String[1];
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String str = "rank";
                    byte[] str_head = str.getBytes();
                    outputStream.write(str_head);
                    in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                    while (true) {
                        receiveMsg[0] = in.readLine();
                        if (receiveMsg[0] != null && receiveMsg[0].length() > 1) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    String[] data1=getonlineuser(receiveMsg[0]);
                                    if(data1==null)return;
                                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                                            lookrankwindow.this,android.R.layout.simple_list_item_1,data1);
                                    ol.setAdapter(adapter);
                                }
                            });
                            break;
                        }
                    }
                    Log.i("wintag", "winisok");
                /*in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
                receiveLoginMsg();
                initgame();*/
                    //initLoginThread();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}